<?php
namespace Home\Controller;
use Think\Controller;
class OrderController extends Controller
{
    // 下单
    public function add()
    {
        // 如果会员没有登录就跳到登录页面，登录成功之后再跳回到这个页面
        $memberId = session('m_id');
        if(!$memberId)
        {
            // 先要登录成功之后要跳回的地址存到SESSION
            session('returnUrl', U('Order/add'));
            redirect(U('Member/login'));
        }
        if(IS_POST)
        {
            $orderModel = D('Admin/Order');
            if($orderModel->create(I('post.'), 1))
            {
                if($orderId = $orderModel->add())
                {
                    $this->success('下单成功!', U('order_success?order_id='.$orderId));
                    exit;
                }
            }
            $this->error($orderModel->getError());
        }

        // 先取出购物车中所有的商品
        $cartModel = D('Cart');
        $data = $cartModel->cartList();//var_dump($data);exit;

        // 取出默认收货地址
        $addressModel = D('Admin/UserAddress');
        $addressData = $addressModel->relation(true)
            ->where(array('member_id' => array('eq', $memberId)))
            ->order('is_default desc,id desc')
            ->find();

        // 取出送货方式
        $shippingModel = M('Shipping');
        $shippingData = $shippingModel->where(
            array('enabled' => array('eq', 1)))
            ->order('shipping_order desc,shipping_id asc')
            ->select();

        //取出发票内容
        $inv_content = C('INV_CONTENT');

        // 设置页面信息
        $this->assign(array(
            'data' => $data,
            'addressData' => $addressData,
            'shippingData' => $shippingData,
            'inv_content' => $inv_content,
            '_page_title' => '定单确认页',
            '_page_keywords' => '定单确认页',
            '_page_description' => '定单确认页',
        ));
        $this->display();
    }

    public function order_success()
    {
        $btn = makeAlipayBtn(I('get.order_id'));
        // 设置页面信息
        $this->assign(array(
            'btn' => $btn,
            '_page_title' => '下单成功',
            '_page_keywords' => '下单成功',
            '_page_description' => '下单成功',
        ));
        $this->display();
    }
}